<?php


namespace Melon;

use App\Controller\HomeController;
use EventHttpRequest;

class Connection
{
    /**
     * @var Application
     */
    protected Application $app;

    /**
     * @var Worker
     */
    protected Worker $worker;

    protected EventHttpRequest $request;

    /**
     * Connection constructor.
     * @param EventHttpRequest $request
     * @param Worker $worker
     * @param Application $application
     */
    public function __construct(EventHttpRequest $request, Worker $worker, Application $application)
    {
        $this->app = $application;

        $this->worker = $worker;

        $this->request = $request;
    }

    public function handle()
    {
        $request = new Request($this->request, $this->worker, $this->app);
        $response = new Response($this->request);

        $return = (new HomeController())->indexAction($request);

        if (is_string($return)) {
            $response->setContent($return);
        }

        $response->send();
    }

    public function __destruct()
    {

    }
}
